﻿namespace GN.Data.Entity.Attribute
{
    public class RegexRegularAttribute : BaseAttribute
    {
        private System.Text.RegularExpressions.RegexOptions _RegexOptions = System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Compiled;
        private string _mailRegular = "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$";
        private string _phoneRegular = "^1[3456789]\\d{9}$";
        /// <summary>
        /// 是否电话
        /// </summary>
        public bool IsPhone = false;
        /// <summary>
        /// 是否邮箱
        /// </summary>
        public bool IsEmail = false;

        /// <summary>
        /// 正则
        /// </summary>
        public string Pattern = string.Empty;

        public RegexRegularAttribute()
        {

        }

        /// <summary>
        /// 正则验证
        /// </summary>
        /// <param name="option"></param>
        public RegexRegularAttribute(string pattern)
        {
            Pattern = pattern;
        }

        /// <summary>
        /// 验证自定义正则
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool IsMatch(string str)
        {
            if (str == null)
            {
                return false;
            }
            return System.Text.RegularExpressions.Regex.IsMatch(str, Pattern, _RegexOptions);
        }

        /// <summary>
        /// 验证邮箱格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool IsMailMatch(string str)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, _mailRegular, _RegexOptions);
        }

        /// <summary>
        /// 验证11位电话格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool IsPhoneMatch(string str)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, _phoneRegular, _RegexOptions);
        }
    }
}
